package com.salt.modules.sys.web;

import com.google.common.collect.Lists;
import com.salt.common.json.AjaxJson;
import com.salt.common.utils.IdGen;
import com.salt.core.log.annotation.OperationRecord;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.*;
import com.salt.modules.sys.service.AppChildMenuService;
import com.salt.modules.sys.service.AppMenuService;
import com.salt.modules.sys.service.MultiTenantService;
import com.salt.modules.sys.service.RightNavBarService;
import com.salt.modules.sys.utils.UserUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 应用菜单(一级目录)Controller
 * @Author salt
 * @Date 2022/1/3
 * @Version 1.0.1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/menu/app")
public class AppMenuController extends BaseController {

    private final AppMenuService appMenuService;
    private final RightNavBarService rightNavBarService;
    private final AppChildMenuService appChildMenuService;
    private final MultiTenantService multiTenantService;

    @GetMapping("list")
    public AjaxJson list(Office office) {
        Map map = multiTenantService.getTenant(office);
        String tenant = (String) map.get("tenant");
        List<String> tenantIds = (List<String>) map.get("tenantIds");
        return AjaxJson.success().put("menuList", appMenuService.getList(tenant, tenantIds));
    }

    @PostMapping("save")
    @OperationRecord(service = "系统服务", module = "应用菜单(一级目录)", operation = "新增", description = "新增应用菜单(一级目录)")
    public AjaxJson save(Menu menu) {
        User user = UserUtils.getUser();
        AppMenu appMenu = new AppMenu(user.getId(), menu.getId(), menu.getTenant());
        AppMenu appmenu = appMenuService.find(appMenu);
        if (appmenu == null) {
            appMenuService.save(appMenu);
            // 自动下载该一级目录下的所有子级菜单（type=1）到sys_app_child_menu
            menu.setUserId(user.getId());
            List<Menu> childMenuList = rightNavBarService.allChildMenuListByByParentIdsLike(menu);
            if (childMenuList != null && !childMenuList.isEmpty()) {
                AppChildMenu appChildMenu = null;
                List<AppChildMenu> appChildMenuList = Lists.newArrayList();
                for (Menu childMenu:childMenuList) {
                    appChildMenu = new AppChildMenu(user.getId(), childMenu.getId(), menu.getId(), menu.getTenant());
                    appChildMenu.setId(IdGen.uuid());
                    appChildMenu.setCreateDate(new Date());
                    appChildMenuList.add(appChildMenu);
                }
                if (!appChildMenuList.isEmpty()) appChildMenuService.batchSave(appChildMenuList);
            }
            return AjaxJson.success("下载成功");
        }
        return AjaxJson.error(menu.getName() + "已下载!");
    }

    /*@PostMapping("batchSave")
    public AjaxJson batchSave(String menuIds) {
        String[] split = menuIds.split(",");
        List<String> ids = Arrays.asList(split);
        appMenuService.batchSave(ids);
        return AjaxJson.success("保存成功");
    }*/

    @DeleteMapping("delete")
    @OperationRecord(service = "系统服务", module = "应用菜单(一级目录)", operation = "删除", description = "删除应用菜单(一级目录)")
    public AjaxJson delete(String menuId) {
        appMenuService.delete(menuId);
        // 删除已经下载的子级
        appChildMenuService.deleteByParentMenuId(menuId);
        return AjaxJson.success("删除成功");
    }



}
